package com.rustic.server.controller.admapi;

import cn.xy.base.server.dto.SysAdminAddDTO;
import cn.xy.base.server.dto.SysAdminUpdateDTO;
import cn.xy.base.server.po.SysAdmin;
import cn.xy.base.server.po.SysPermission;
import cn.xy.base.server.po.SysRole;
import cn.xy.base.server.service.SysAdminService;
import cn.xy.base.server.vo.SysAdminVO;
import cn.xy.base.server.vo.SysRoleVO;
import cn.xy.base.server.web.AdminWebContext;
import cn.xy.commons.annotation.RepeatSubmit;
import cn.xy.commons.constant.Consts;
import com.rustic.server.query.RusticAdminListQuery;
import com.rustic.server.dto.RusticAddDTO;
import com.rustic.server.dto.RusticUpdateDTO;
import com.rustic.server.po.Rustic;
import com.rustic.server.service.RusticService;
import com.rustic.server.vo.RusticAdminDetailVO;
import com.rustic.server.vo.list.RusticAdminListVO;
import com.rustic.server.vo.RusticVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.annotation.OperateLog;
import cn.xy.commons.core.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 表[rustic]对应的后台管理类
 *
 * @author auto
 */

@Slf4j
@RestController
@RequestMapping("/admapi/rustic")
@Api(tags = {"乡村管理接口"})
public class RusticAdminController {
    // TODO 进入的权限判断

    @Autowired
    private RusticService rusticService;

    @Autowired
    private SysAdminService sysAdminService;

    @ApiOperation(value = "乡村-列表", notes = "权限key: rustic-list")
    @GetMapping("/list")
//    @PreAuthorize("@validate.hasPermission('rustic-list')")
    public Result<Page<RusticAdminListVO>> list(@Validated RusticAdminListQuery query) {
        return Result.data(rusticService.getAdminPageList(query, AdminWebContext.getAdminId()));
    }

    @ApiOperation(value = "乡村-详情", notes = "权限key: rustic-detail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "乡村id"),
    })
    @GetMapping("/detail")
    @PreAuthorize("@validate.hasPermission('rustic-detail')")
    public Result<RusticVO> detail(int id) {
        if (id <= 0) {
            return Result.error(RusticService.RUSTIC_NOT_EXIST);
        }
        Rustic rustic = rusticService.getById(id);
        if (rustic == null) {
            return Result.error(RusticService.RUSTIC_NOT_EXIST);
        }
        RusticVO rusticVO = new RusticVO(rustic);
        return Result.data(rusticVO);
    }

    @ApiOperation(value = "乡村-增加", notes = "权限key: rustic-add")
    @PostMapping("/add")
    @PreAuthorize("@validate.hasPermission('rustic-add')")
    @OperateLog(description = "增加乡村")
    public Result<String> add(@RequestBody @Validated RusticAddDTO rusticAddDTO) {
        Rustic rustic = rusticAddDTO.toRustic();
        rusticService.save(rustic);
        return Result.ok("添加乡村成功");
    }

    @ApiOperation(value = "乡村-更新", notes = "权限key: rustic-update")
    @PutMapping("/update")
    @PreAuthorize("@validate.hasPermission('rustic-update')")
    @OperateLog(description = "更新乡村")
    public Result<String> update(@RequestBody @Validated RusticUpdateDTO rusticUpdateDTO) {
        Rustic rustic = rusticService.getById(rusticUpdateDTO.getId());
        if (rustic == null) {
            return Result.error(RusticService.RUSTIC_NOT_EXIST);
        }
        BeanUtils.copyProperties(rusticUpdateDTO, rustic);
        rusticService.updateById(rustic);
        return Result.ok("编辑乡村成功");
    }

    @ApiOperation(value = "乡村-删除", notes = "权限key: rustic-delete")
    @DeleteMapping("/delete")
    @PreAuthorize("@validate.hasPermission('rustic-delete')")
    @OperateLog(description = "删除乡村")
    public Result<String> delete(int id) {
        if (id <= 0) {
            return Result.error(RusticService.RUSTIC_NOT_EXIST);
        }
        // 判断是否有权限删除乡村
        rusticService.isRusticAdmin(id, AdminWebContext.getAdminId());
        Rustic rustic = rusticService.getById(id);
        if (rustic == null) {
            return Result.error(RusticService.RUSTIC_NOT_EXIST);
        }
        rusticService.removeById(id);
        return Result.ok("删除乡村成功");
    }

    @ApiOperation(value = "乡村-禁用启用", notes = "权限key: rustic-disable")
    @PostMapping("/disable")
    @PreAuthorize("@validate.hasPermission('rustic-disable')")
    @OperateLog(description = "禁用启用乡村")
    public Result<String> disable(int id) {
        if (id <= 0) {
            return Result.error(RusticService.RUSTIC_NOT_EXIST);
        }
        rusticService.disable(id, AdminWebContext.getAdminId());
        return Result.ok("修改乡村状态成功");
    }

    @ApiOperation(value = "增加乡村运营管理员账号")
    @PostMapping("/admin/add")
    @OperateLog(description = "增加乡村运营管理员账号")
    @PreAuthorize("@validate.hasPermission('system-user-addRusticAdmin')")
    @RepeatSubmit
    public Result<String> addRusticAdmin(@Validated @RequestBody SysAdminAddDTO sysAdminAddDTO) {
        sysAdminService.addRusticOrOperation(sysAdminAddDTO, AdminWebContext.getAdminId());
        return Result.ok("添加管理员成功");
    }

    @ApiOperation(value = "更新乡村运营管理员账号")
    @PostMapping("/admin/update")
    @OperateLog(description = "更新乡村运营管理员账号")
    @PreAuthorize("@validate.hasPermission('system-user-updateRusticAdmin')")
    @RepeatSubmit
    public Result<String> updateRusticAdmin(@Validated @RequestBody SysAdminUpdateDTO sysAdminUpdateDTO) {
        sysAdminService.updateRusticOrOperation(sysAdminUpdateDTO, AdminWebContext.getAdminId());
        return Result.ok("修改管理员成功");
    }

    @ApiOperation(value = "乡村运营管理员账号详情")
    @GetMapping("/admin/detail")
    @OperateLog(description = "乡村运营管理员账号详情")
    @PreAuthorize("@validate.hasPermission('system-user-detailRusticAdmin')")
    public Result<RusticAdminDetailVO> detailRusticAdmin(int adminId, int rusticId) {
        RusticAdminDetailVO rusticAdmin = sysAdminService.getRusticAdmin(adminId, rusticId);
        return Result.data(rusticAdmin);
    }

    @ApiOperation(value = "乡村运营管理员账号详情-进入乡村")
    @PostMapping("/admin/getRusticAdmin")
    public Result<RusticAdminDetailVO> getRusticAdmin(int rusticId) {
        RusticAdminDetailVO rusticAdmin = sysAdminService.getRusticAdmin(AdminWebContext.getAdminId(), rusticId);
        return Result.data(rusticAdmin);
    }
}
